import {
  Form
} from 'antd-mini/es/Form/form';
import {
  workOderAudit,
  getContractNamesByProjectName,
  getIoCompany,
  dispatchOrder,
  getProjectName
} from '/service/service'
Page({
  data: {
    contractName: '',
    projectName: '',
    handle: '',
    handleList: ['事件派遣', '事件转派', '事件作废'],
    maintenanceUnit: '',
    maintenanceRequirement: '',
    maintenanceRequirementList: ['修复', '新增', '拆除', '清除', '无', '其他'],
    dispatchOpinion: '',
    id: '',
    workOderAuditInfo: {},
    isNotInvalid: true,
    projectNameList: []
  },
  onLoad(option) {
    this.form = new Form({
      rules: {
        projectName: [{
          required: true,
          message: '请选择项目名称',
        }],
        contractName: [{
          required: true,
          message: '请选择所属合同',
        }],
        handle: [{
          required: true,
          message: '请选择操作处理',
        }],
        maintenanceRequirement: [{
          required: true,
          message: '请选择维护要求',
        }],
        dispatchOpinion: [{
          required: true,
          message: '请输入派遣意见',
        }]
      },
    })
    const {
      id
    } = option
    this.setData({
      id
    })

    this.getProjectNameList()
    this.getWorkOderAuditInfo(id)
    // this.getContractName()
  },
  handleRef(ref) {
    this.form.addItem(ref);
  },
  getProjectNameList() {
    getProjectName().then(res => {
      if (res && Array.isArray(res) && res.length > 0) {
        const projectNameList = []
        res.forEach(item => projectNameList.push(item.projectName))
        this.setData({
          projectNameList
        })
        // console.log(this.data.projectNameList)
      }
    })
  },
  projectNameControlledOk(e) {
    this.form.setFieldValue('projectName', e)
    this.form.setFieldValue('contractName', null)
    this.form.setFieldValue('maintenanceUnit', null)
    this.getContractNamesByProjectNameList(e)
  },
  getWorkOderAuditInfo() {
    workOderAudit(this.data.id).then(res => {
      if (res && Object.keys(res).length > 0) {
        const {
          projectName
        } = res
        this.setData({
          projectName
        })
        this.form.setFieldValue('projectName', projectName)
        this.getContractNamesByProjectNameList(projectName)
      }
    })
  },
  async getContractNamesByProjectNameList(projectName) {
    try {
      const res = await getContractNamesByProjectName({
        projectName
      })
      console.log(res)
      if (res && Array.isArray(res) && res.length > 0) {
        const contractNameList = []
        res.forEach(item => contractNameList.push(item.contractName))
        this.setData({
          contractNameList
        })
      }
    } catch (e) {
      console.error('请求合同列表错误')
    }
  },
  contractNameControlledOk(e) {
    getIoCompany({
      name: e
    }).then(res => {
      if (res) {
        this.setData({
          maintenanceUnit: res
        })
        this.form.setFieldValue('maintenanceUnit', res)
      }
    })
  },
  handleControlledOk(e) {
    if (e === '事件作废') {
      this.setData({
        isNotInvalid: false
      })
    }
  },
  async submit() {
    try {
      const values = await this.form.submit();
      const {
        id
      } = this.data
      const params = {
        ...values,
        id
      }
      console.log(params)
      const res = await dispatchOrder(params)
      if (res) {
        my.showToast({
          content: '派遣成功',
          type: 'success',
          duration: 2000
        })
        my.navigateBack();
      }
    } catch (e) {
      my.showToast({
        content: '派遣失败,请检查',
        type: 'fail',
        duration: 2000
      })
    }

  },
  cancel() {
    my.navigateBack();
  }
});